package com.example.myapplication.retrofit

import android.widget.ListAdapter
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.myapplication.test1
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.orhanobut.logger.Logger
import io.reactivex.Observable
import io.reactivex.Observer
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory

/**
 * 类描述:
 * 创建人:   tangchao$
 * 创建时间: 2021/7/27$ 8:44 PM$
 * 联系方式: 419704299@qq.com
 * 修改人:   tangchao$
 * 修改时间: 2021/7/27$ 8:44 PM$
 * 修改备注:  [说明本次修改内容]
 */
class RetrofitViewModel() : ViewModel() {
    var gson = GsonBuilder().serializeNulls().create()

    //            .registerTypeHierarchyAdapter(List::class.java,ListAdapter())
    var retrofit = Retrofit.Builder()
            .baseUrl("https://www.wanandroid.com")
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build()


    fun getData() {
        viewModelScope.launch {
            retrofit.create(WanandroidServe::class.java)

            var result = retrofit.create(WanandroidServe::class.java).login("123", "123")
            Logger.d("TestRetrfitActivity===initData: " + result);
            result.enqueue(object : Callback<ResponseBody> {
                override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
                    Logger.d("TestRetrfitActivity===onResponse: " + response.body());
                    Logger.d("TestRetrfitActivity===onResponse: " + response.raw());

                }

                override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
                    Logger.d("===: ");

                }
            })
        }
    }

    fun getarticle() {
        viewModelScope.launch {
            val create = retrofit.create(WanandroidServe::class.java)
            val article = create.article(0)
            article.enqueue(object : Callback<ResponseBody> {
                override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
                    Logger.d("RetrofitViewModel===onResponse: response=" + response);

                }


                override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
                    Logger.d("RetrofitViewModel===onResponse: t=" + t.message);
                }
            })
        }

    }

    fun getarticle1() {
        viewModelScope.launch {
            var a = async {
                test11()
            }
            var b = async {
                test11()
            }

        }

    }

    suspend fun test11() {
        withContext(Dispatchers.IO) {
            "34567"
        }
    }

}